import axios from 'axios'
import qs from 'qs'
import router from '../../router'
import store from '../../store/store'
import { Loading, MessageBox, table} from 'element-ui'
let needLoadingRequestCount = 0
export function showFullScreenLoading () {
  if (needLoadingRequestCount === 0) {
    // startLoading()
  }
  needLoadingRequestCount++
}
export function tryHideFullScreenLoading () {
  if (needLoadingRequestCount <= 0) return
  needLoadingRequestCount--
  if (needLoadingRequestCount === 0) {
    // endLoading()
  }
}

let loading
function startLoading () {
  loading = Loading.service({
		lock: false,
    text: '加载中……',
    background: 'rgba(0, 0, 0, 0.8)',
		fullscreen: true,
  })
}
function endLoading () {
  loading.close()
}

axios.interceptors.request.use(function (config) {
  showFullScreenLoading()
  return config
}, error => {
  return Promise.resolve(error)
})

axios.interceptors.response.use((response) => {
  tryHideFullScreenLoading()
  return response
}, error => {
  return Promise.resolve(error)
})

function checkStatus (response) {
  if (response && ((response.status === 200 || response.status === 304 || response.status === 400))) {
    return response.data
  }
  return {
    code: '404',
    message: '网络异常'
  }
}
// let api = '/api'
let api = process.env.NODE_ENV === 'development' ? '/api' : ''
export default {
  api,
  router,
  messageTwo (message, title) {
    MessageBox.alert(message, title, {
      confirmButtonText: '确定'
    })
  },
  alert (message, title, func) {
    MessageBox.alert(message, title, {
      confirmButtonText: '确定',
      callback: action => {
        func()
      }
    })
  },
	get (url, data) {
		return axios.get( api + url, {
      params: data,
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      },
		}).then((res) => {
			// return checkStatus(res)
			if (res.data.code !== 10200) {
        if (res.data.msg === '未登录！') {
          router.push({ name: "login" });
        } else {
          MessageBox.alert(res.data.msg, '提示信息', {
            confirmButtonText: '确定'
          })
        }
				return false
      }
      if (res.data.code === 10200) {
        return checkStatus(res)
      }
		}).catch(function () {
			tryHideFullScreenLoading()
			MessageBox.alert('系统错误，联系管理员', '提示信息', {
				confirmButtonText: '确定',
				callback: action => {
				}
			})
		})
	},
  post (url, data) {
    return axios({
      method: 'post',
      baseURL: process.env.BASE_URL,
      url: api + url,
      data: qs.stringify(data),
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      },
    }).then((res) => {
      if (res.data.code !== 10200) {
        MessageBox.alert(res.data.msg, '提示信息', {
          confirmButtonText: '确定'
        })
        return false
      }
      if (res.data.code === 10200) {
        return checkStatus(res)
      }
    }).catch(function () {
      tryHideFullScreenLoading()
      MessageBox.alert('系统错误，联系管理员', '提示信息', {
        confirmButtonText: '确定',
        callback: action => {
        }
      })
    })
  }
}
